package com.applancer.nativecmsflip;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.applancer.dataprovider.DatabaseHandler;
import com.applancer.fragment.FeedDetailFragment;
import com.applancer.model.FeedModel;
import com.applancer.provider.SystemBarTintManager;
import com.applancer.ui.CustomViewPager;

public class FeedDetailActivity extends Activity {

	protected String title;
	protected int currentIndex;
	protected DatabaseHandler sqlite;
	protected Menu actionbarMenu;
	protected CustomViewPager feed_detail_pager;
	protected ScreenSlidePagerAdapter adapter;
	protected List<FeedModel> ldata;
	protected FeedModel currentFeed;
	protected ShareActionProvider mShareActionProvider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.feed_detail_activity);
		initial();
		execution();
	}

	private void initial() {

		ldata = new ArrayList<FeedModel>();

		feed_detail_pager = (CustomViewPager) findViewById(R.id.feed_detail_pager);

		ldata = getIntent().getParcelableArrayListExtra("lmodel");
		currentIndex = getIntent().getExtras().getInt("index");
		title = getIntent().getExtras().getString("title").toString();

		if (ldata.size() > 0)
			currentFeed = ldata.get(currentIndex);

		adapter = new ScreenSlidePagerAdapter(getFragmentManager(), ldata);

		sqlite = new DatabaseHandler(this);
	}

	private void execution() {

		feed_detail_pager.setAdapter(adapter);
		feed_detail_pager.setCurrentItem(currentIndex, false);
		feed_detail_pager.setOffscreenPageLimit(1);
		feed_detail_pager.setOnPageChangeListener(onpagechange);

		getActionBar().setIcon(R.drawable.ic_detail_feed_back);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setTitle(title);

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setNavigationBarTintEnabled(true);

		tintManager.setStatusBarTintResource(R.color.statusbar_bg);
		tintManager.setNavigationBarTintResource(R.color.actionbar_bg);

	}

	OnPageChangeListener onpagechange = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			currentFeed = ldata.get(arg0);

			if (sqlite.getModel(Integer.parseInt(currentFeed.getId())) != null)
				actionbarMenu.getItem(1).setIcon(
						R.drawable.ic_detail_feed_mark_disable);
			else
				actionbarMenu.getItem(1)
						.setIcon(R.drawable.ic_detail_feed_mark);

			invalidateOptionsMenu();

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

	private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {

		protected List<FeedModel> ldata;

		public ScreenSlidePagerAdapter(FragmentManager fm, List<FeedModel> data) {
			super(fm);
			ldata = data;
		}

		@Override
		public Fragment getItem(int position) {

			FeedModel m = ldata.get(position);
			FeedDetailFragment fragment = new FeedDetailFragment(m);
			return fragment;

		}

		@Override
		public int getCount() {
			return ldata.size();
		}
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		int itemId = item.getItemId();
		switch (itemId) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.action_comment: {
			Intent intent = new Intent(FeedDetailActivity.this,
					CommentDialogActivity.class);

			intent.putExtra("id", currentFeed.getId().toString());
			intent.putExtra("urlpost", currentFeed.getUrl());

			startActivity(intent);
			break;
		}
		case R.id.action_mark:

			if (sqlite.addModel(currentFeed)) {

				Toast.makeText(this, getString(R.string.bm_success),
						Toast.LENGTH_SHORT).show();
				actionbarMenu.getItem(1).setIcon(
						R.drawable.ic_detail_feed_mark_disable);
				invalidateOptionsMenu();
			} else {
				sqlite.deleteModel(currentFeed);
				actionbarMenu.getItem(1)
						.setIcon(R.drawable.ic_detail_feed_mark);
				invalidateOptionsMenu();
			}

			break;
		case R.id.action_share:

			Intent sendIntent = new Intent();
			sendIntent.setAction(Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT,
					"Newfeed application in android.");
			sendIntent.setType("text/plain");
			startActivity(sendIntent);

			break;
		}

		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.feed_detail, menu);

		MenuItem item = menu.findItem(R.id.action_share);

		// Fetch and store ShareActionProvider
		mShareActionProvider = (ShareActionProvider) item.getActionProvider();
		// mShareActionProvider.setShareIntent(getDefaultIntent());

		actionbarMenu = menu;
		if (sqlite.getModel(Integer.parseInt(currentFeed.getId())) != null)
			actionbarMenu.getItem(1).setIcon(
					R.drawable.ic_detail_feed_mark_disable);
		else
			actionbarMenu.getItem(1).setIcon(R.drawable.ic_detail_feed_mark);

		return super.onCreateOptionsMenu(menu);
	}

	// Call to update the share intent
	protected void setShareIntent(Intent shareIntent) {
		if (mShareActionProvider != null) {
			mShareActionProvider.setShareIntent(shareIntent);
		}
	}

}
